﻿@page "/fetchdata"
@using EmployeeLeaveSystem.Shared
@inject HttpClient Http
@inject IMessageService _message
@implements IPageFlush;
@using EmployeeLeaveSystem.Shared.Models;
@using System.ComponentModel.DataAnnotations;
@using System.ComponentModel.DataAnnotations.Schema;


<PageTitle>请假历史记录</PageTitle>

@if (GobalManager.myInfo == null) {
    <div>
        <Alert ShowIcon="true" Message="您尚未登录，点击右上角的“登录”来登录。" Type="@AlertType.Error" />
    </div>
} else {
    <div style="width:100%">
        <Title Level="1">请假历史记录</Title>
        <Paragraph>
            <Text>在这里查询您的个人请假历史记录</Text>
            <Statistic Title="所有记录" Value="@GobalManager.myInfo.LeaveDetail.Count" />
        </Paragraph>
        @{
            var msg = "获取消息成功";
        }
        <p>当前刷新的时间点：@DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")</p>
        <Button OnClick="()=>{DoHistorySort();_message.Info(msg);}">刷新</Button>

        <p />
        <Title Level="3">正在发生</Title>
        <Paragraph>当前正在进行的请假事项，您必须要进行销假，才能发出信的申请。</Paragraph>
        <Paragraph>
            @{
                string _back_color = PresetColor.Orange.ToString();
                string _back_Title = "预期销假";
                if (nowDetai?.AcceptFlag == AcceptFlag.审核中) { //等待审核
                    _back_color = PresetColor.Orange.ToString();
                    _back_Title = "等待审核";
                } else { // 正在休假
                    _back_color = PresetColor.Green.ToString();
                    _back_Title = "正在休假";
                }
                if (nowDetai?.AcceptFlag == AcceptFlag.接受 && DateTime.Now > nowDetai.EndTime) {
                    _back_color = PresetColor.Red.ToString();
                    _back_Title = "销假逾期";
                }
            }
            <Collapse>
                @if (nowDetai == null) {
                    <Alert Type="@AlertType.Info" Message="当前无活动" />
                } else {
                    <Panel Header="@nowDetai?.Name" Key="1" Active="true">
                        <ExtraTemplate>
                            <div @onclick:stopPropagation><Button OnClick="()=>{LeaveDetailsInfo.NavTo(nowDetai.Id);}">查看</Button></div>
                        </ExtraTemplate>
                        <ChildContent>
                            <div style="width:100%;height:20px;background-color:@_back_color">
                                <div class="BackMoveAni"></div>
                            </div>
                            <h2 style="margin:0;color:@_back_color">@_back_Title</h2>
                            <p />
                            <GridRow>
                                <GridCol Span="4">
                                    <Text Strong>详细信息</Text>
                            </GridCol>
                            <GridCol>
                                <Text>@nowDetai?.Description</Text>
                            </GridCol>
                        </GridRow>

                        <GridRow>
                            <GridCol Span="4">
                                <Text Strong>请假时间</Text>
                            </GridCol>
                            <GridCol>
                                <Text>@nowDetai?.StartTime.ToString() 至 @nowDetai?.EndTime.ToString()</Text>
                            </GridCol>
                        </GridRow>

                        <GridRow>
                            <GridCol Span="4">
                                <Text Strong>审核人</Text>
                            </GridCol>
                            <GridCol>
                                <Text>@nowDetai?.AcceptStaff?.Name</Text>
                            </GridCol>
                        </GridRow>

                        <h3>最新消息</h3>
                        @if (nowDetai?.Comments.Count > 0) {

                                var msgNew = nowDetai.Comments[nowDetai.Comments.Count - 1];

                                <Comment Author="@msgNew.StaffInfo.Name"
                             Avatar="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
                             Content="@msgNew.Msg"
                             Datetime="@msgNew.Time.ToString()">
                                </Comment>
                            } else {
                                <Alert>当前尚无消息</Alert>
                            }
                            @if (nowDetai?.AcceptFlag == AcceptFlag.接受) {
                                <h3>执行动作</h3>
                                <p>当前您应该进行销假，点击销假后，可以填写相应的信息。</p>
                                <Button Type="@ButtonType.Primary" Block="true" OnClick="()=>{
                                    App._appHandle?.XiaoJiaLeaveDetail(nowDetai.Id);
                                        }">
                                    销假
                                </Button>
                            }
                        </ChildContent>
                    </Panel>
                }
            </Collapse>
        </Paragraph>
        <Paragraph>

        </Paragraph>
        <Title Level="3">所有的历史记录</Title>
        <Table @ref=now
           TItem="LeaveDetail"
           DataSource="allDates"
           ScrollX="130%">
            <PropertyColumn Width="70" Title="编号" Property="x=>x.Id" Fixed="left"></PropertyColumn>
            <PropertyColumn Width="200" Title="标题" Property="x=>x.Name"></PropertyColumn>
            <PropertyColumn Width="200" Title="开始时间" Property="x=>x.StartTime"></PropertyColumn>
            <PropertyColumn Width="200" Title="结束时间" Property="x=>x.EndTime"></PropertyColumn>
            <PropertyColumn Width="80" Title="审核人" Property="x=>x.AcceptStaff.Name"></PropertyColumn>
            <PropertyColumn Width="80" Title="类型" Property="x=>x.Type">
                @switch (context.Type) {
                    case LeaveType.CasualLeave: {
                            <div>事假</div>
                            break;
                        }
                    case LeaveType.SickLeave: {
                            <div>病假</div>
                            break;
                        }
                    case LeaveType.Business: {
                            <div>出差</div>
                            break;
                        }
                    case LeaveType.PaidLeave: {
                            <div>带薪休假</div>
                            break;
                        }
                }
            </PropertyColumn>
            <PropertyColumn Width="80" Title="当前" Property="x=>x.AcceptFlag">
                @switch (context.AcceptFlag) {
                    case AcceptFlag.审核中: {
                            <Tag Color="@PresetColor.Orange.ToString()">等待审核</Tag>
                            break;
                        }
                    case AcceptFlag.接受: {
                            <Tag Color="@PresetColor.Green.ToString()">正在休假</Tag>
                            break;
                        }
                    case AcceptFlag.正常结束: {
                            <Text>已完成</Text>
                            break;
                        }
                }
            </PropertyColumn>
            <ActionColumn Title="操作" Fixed="right" Width="100"><Button OnClick="()=>{LeaveDetailsInfo.NavTo(context.Id);}"> 查看 </Button></ActionColumn>
        </Table>
    </div>
}

<style>
    /*横条移动CSS*/
    .BackMoveAni {
        background-image: url('./Resources/滚动条2.png');
        height: 100%;
        width: 100%;
        background-repeat-x: repeat;
        background-size: contain;
        background-position-x: 0px;
        animation-name: Ani;
        animation-timing-function: linear;
        animation-iteration-count: infinite;
        animation-duration: 15s;
    }

    @@keyframes Ani {
        to {
            background-position-x: 104%;
        }
    }
</style>

@code {
    private List<LeaveDetail> allDates;
    private LeaveDetail? nowDetai = null;
    ITable now, others;
    protected override void OnInitialized() {
        GobalManager.AddPage(this);
        base.OnInitialized();
    }
    protected override async Task OnInitializedAsync() {
        DoHistorySort();
    }

    public void Flush() {
        DoHistorySort();
    }
    async void DoHistorySort() { //将获得的历史记录进行相应的填充
        await App._appHandle.GetUserHistory();
        if (GobalManager.myInfo == null && GobalManager.myInfo?.LeaveDetail == null) return;
        allDates = GobalManager.myInfo.LeaveDetail;
        nowDetai = null;
        if (allDates?.Count > 0) {
            var data = allDates[allDates.Count - 1];
            if (data != null) {
                if ((DateTime.Now >= data.StartTime || data.AcceptFlag == AcceptFlag.审核中) && data.AcceptFlag != AcceptFlag.正常结束) { // 还在假期中
                    var netres = await Http.GetFromJsonAsync<List<Comments>>($"Main/GetCommentsByLeavesId?id={data.Id}");
                    if (netres != null) data.Comments = netres;
                    if (netres?.Count > 0) { // 填充个人的信息

                        var res2 = await Http.GetFromJsonAsync<StaffInfo>($"Main/GetStaffInfoById?id={data.Comments[data.Comments.Count - 1].StaffInfoId}");
                        if (res2 != null)
                            data.Comments[data.Comments.Count - 1].StaffInfo = res2;

                    }
                    // 将最后一个信息索引到nowDetail
                    nowDetai = data;
                }
            }
        }
        StateHasChanged();
    }
}
